﻿using QMesSocketLib;
using System;
using System.IO;
using System.IO.Ports;
using System.Xml;

namespace QMesSocketServer
{
    public class Settings
    {

        public static XmlDocument _XmlDocument = null;

        public static XmlDocument GeteXmlDoc()
        {
            if (_XmlDocument != null)
            {
                return _XmlDocument;
            }
            else
            {
                string file = GetSettingFileName();
                if (!File.Exists(file))
                {
                    CreateSettingForlder();
                }
                else
                {
                    _XmlDocument = new XmlDocument();
                    _XmlDocument.Load(file);
                }
            }

            return _XmlDocument;
        }

        #region 配置文件XML
        static void CreateSettingForlder()
        {
            string path = GetSettingPath();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }


        public static string GetSettingPath()
        {
            return Path.Combine(System.Environment.CurrentDirectory, "Settings");
        }


        public static void CreateSettingFile()
        {
            CreateSettingForlder();

            string file = GetSettingFileName();
            XmlDocument xmlDoc = new XmlDocument();
            if (!File.Exists(file))
            {
                //创建类型声明节点
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
            }
            else
            {
                xmlDoc.Load(file);
            }

            //创建根节点
            XmlNode root = xmlDoc.SelectSingleNode("Settings");
            if (root == null)
            {
                root = xmlDoc.CreateElement("Settings");
                xmlDoc.AppendChild(root);
            }

            //LCR仪COM口设置
            XmlNode comNode = root.SelectSingleNode("SocketServer");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("SocketServer");
                root.AppendChild(comNode);
            }
            CreateNode(xmlDoc, comNode, "Address", "127.0.0.1"); 
            CreateNode(xmlDoc, comNode, "Port", "9002"); 

            _XmlDocument = xmlDoc;
            xmlDoc.Save(file);
        }

        public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            if (parentNode.SelectSingleNode(name) != null) return;
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, string.Empty);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }

        public static string GetSettingFileName()
        {
            return Path.Combine(System.Environment.CurrentDirectory, "Settings\\Settings.xml");
        }

        public static XmlNode GetXmlNode(string name)
        {
            XmlNode node = null;
            XmlDocument xmlDoc = GeteXmlDoc();

            node = xmlDoc.SelectSingleNode(name);
            if(node == null)
            {
                CreateSettingFile();
            }

            return node;
        }
        #endregion 配置文件XML

        #region COM口
        public static XmlNode GetSocketServerXmlNode()
        {
            return Settings.GetXmlNode("/Settings/SocketServer");
        }

        public static string GetSocketServerAddress()
        {
            string address = "127.0.0.1";

            try
            {
                XmlNode node = GetSocketServerXmlNode();
                if (node != null) address = node.SelectSingleNode("Address").InnerText;
            }
            catch(Exception ex)
            {
                Log.LogError(ex);
            }
            return address;
        }

        public static int GetSocketServerPort()
        {
            int port = 9002;

            try
            {
                XmlNode node = GetSocketServerXmlNode();
                if (node != null)
                {
                    string portS = node.SelectSingleNode("Port").InnerText;
                    if (!string.IsNullOrWhiteSpace(portS))
                        int.TryParse(portS, out port);
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex);
            }
            return port;
        }

        #endregion Com口
    }
}
